﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VTFC
{
    // ----------------------------------------------------------------------------
    /// <summary>
    /// <see cref="VTFC.Server"/> property editor.
    /// </summary>
    // ----------------------------------------------------------------------------
    public partial class FrmServerShow : Form
    {
        /// <summary><see cref="VTFC.Server"/> instance for editing.</summary>
        public Server Server { get; set; }

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Constructor.
        /// </summary>
        // ----------------------------------------------------------------------------
        public FrmServerShow()
        {
            InitializeComponent();
            InitializeContent();
        }//ctor

        private void InitializeContent()
        {
            this.Text = Resources.FrmServerShow_Text;
            this.lblCheckInType.Text = Resources.FrmServerShow_lblCheckInType;
            this.lblDomain.Text = Resources.FrmServerShow_lblDomain;
            this.lblPassword.Text = Resources.FrmServerShow_lblPassword;
            this.lblUrl.Text = Resources.FrmServerShow_lblUrl;
            this.lblUserName.Text = Resources.FrmServerShow_lblUserName;
            this.lblWorkDirectory.Text = Resources.FrmServerShow_lblWorkDirectory;
            this.lblShowUnControlledFiles.Text = Resources.FrmServerShow_lblShowUnControledFiles;
            this.btnCancel.Text = Resources.ButtonCancel;
            this.btnOk.Text = Resources.ButtonOk;
        }//func InitializeContent

        // ----------------------------------------------------------------------------
        /// <summary>
        /// Databind controls.
        /// </summary>
        // ----------------------------------------------------------------------------
        public void Initialize()
        {
            txtDomain.Text                      = Server.Domain;
            txtPassword.Text                    = BaseLogic.DecryptString(Server.Password);
            txtUrl.Text                         = Server.Url;
            txtUserName.Text                    = Server.UserName;
            txtWorkDirectory.Text               = Server.WorkDircetory;
            ckbNetworkAuthentication.Checked    = !Server.UseDefaultCredentials;
            ckbShowUnControlledFiles.Checked    = Server.ShowUnControlledFiles;

            cmbCheckInType.DataSource           = Enum.GetValues(typeof(CheckinType));
            cmbCheckInType.SelectedItem         = Server.DefaultCheckinType;
        }//func Initialize

        private void btnSelectWorkDirectory_Click(object sender, EventArgs e)
        {
            string folderpath = BaseLogic.SelectFolder(null);
            if (folderpath != null)
            {
                txtWorkDirectory.Text = folderpath;
            }
        }//func btnSelectWorkDirectory_Click

        private void btnOk_Click(object sender, EventArgs e)
        {
            Server.Url = txtUrl.Text.Trim();
            Server.UseDefaultCredentials = !ckbNetworkAuthentication.Checked;
            Server.Domain = txtDomain.Text.Trim();
            Server.Password = BaseLogic.EncryptString(txtPassword.Text.Trim());
            Server.UserName = txtUserName.Text.Trim();
            Server.WorkDircetory = txtWorkDirectory.Text.Trim();
            Server.DefaultCheckinType = (CheckinType)Enum.Parse(typeof(CheckinType), cmbCheckInType.Text);
            Server.ShowUnControlledFiles = ckbShowUnControlledFiles.Checked;
            
            this.DialogResult = DialogResult.OK;
        }//func btnOk_Click

        private void ckbNetworkAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            gpbNetwork.Enabled = ckbNetworkAuthentication.Checked;
        }//func ckbNetworkAuthentication_CheckedChanged
    }//class FrmServerShow
}//namespace VTFC
